package com.nirvana.item;

import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.awt.RelativePoint;
import com.nirvana.data.DependenceMetaData;
import com.nirvana.type.ItemType;
import com.nirvana.ui.TooltipEditPanel;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.function.Supplier;

public class ResolvePropertyItem extends AbstractItem{
    private static final String[] DEFAULT_PARAM_TYPE = new String[]{"String", "MapBean"};
    private final JTextField resolveMethodTextField = new JTextField();
    private final JButton tooltipButton = new JButton();
    private final JComboBox<String> paramTypeComboBox = new JComboBox<>(new DefaultComboBoxModel<>(DEFAULT_PARAM_TYPE));
    private final JCheckBox showCheckBox = new JCheckBox();
    private String tooltip = "";

    @Override
    public void init() {
        super.init();
        tooltipButton.setText("...");
        tooltipButton.addActionListener(e -> TooltipEditPanel.getInstance().open(this));
        tooltipButton.addMouseListener(getMouseListener(this::getToolTip,  Balloon.Position.atLeft));
        showCheckBox.setSelected(true);
        showCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
        int column = 2;
        itemPanel.add(resolveMethodTextField, getConstraintsStr(column++));
        itemPanel.add(paramTypeComboBox, getConstraintsStr(column++));
        itemPanel.add(showCheckBox, getConstraintsStr(column++));
        itemPanel.add(tooltipButton, getConstraintsStr(column++));
    }

    private MouseAdapter getMouseListener(Supplier<String> popupTextSupplier, Balloon.Position position) {
        return new MouseAdapter() {
            Balloon balloon;
            @Override
            public void mouseEntered(MouseEvent e) {
                balloon = JBPopupFactory.getInstance()
                        .createHtmlTextBalloonBuilder(popupTextSupplier.get(), MessageType.INFO, null)
                        .setHideOnKeyOutside(true)
                        .setHideOnLinkClick(true)
                        .setAnimationCycle(150)
                        .createBalloon();
                RelativePoint showPoint = new RelativePoint(e);
                balloon.show(showPoint, position);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                balloon.hide();
            }
        };
    }

    @Override
    protected ItemType getItemType() {
        return ItemType.ResolvePropertyItem;
    }

    @Override
    public void copy(AbstractItem item) {
        if (item instanceof ResolvePropertyItem resolvePropertyItem) {
            propertyNameTextField.setText(resolvePropertyItem.getPropertyName());
            resolveMethodTextField.setText(resolvePropertyItem.getResolveMethod());
            paramTypeComboBox.setSelectedIndex(resolvePropertyItem.getParamTypeIndex());
            showCheckBox.setSelected(resolvePropertyItem.showCheckBox.isSelected());
            tooltip = resolvePropertyItem.getToolTip();
        }
    }

    public String getResolveMethod() {
        return resolveMethodTextField.getText();
    }

    public int getParamTypeIndex() {
        return paramTypeComboBox.getSelectedIndex();
    }

    public int getShow() {
        return showCheckBox.isSelected() ? 1 : 0;
    }

    public String getToolTip() {
        return tooltip;
    }

    public void setTooltip(String tooltip) {
        this.tooltip = tooltip;
    }

    public void populate(String propertyName, DependenceMetaData data) {
        propertyNameTextField.setText(propertyName);
        resolveMethodTextField.setText(data.getResolveMethod());
        paramTypeComboBox.setSelectedIndex(data.getParamTypeIndex());
        showCheckBox.setSelected(data.getShow() == 1);
        this.tooltip = data.getTooltip();
    }
}
